package com.threeonemeeting.activity;

import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.util.MiscHelper;

public class BaseActivity extends Activity {

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		Activity parent = getParent();
		if (parent == null) {
			super.startActivityForResult(intent, requestCode);
			return;
		}
		parent.startActivityForResult(intent, requestCode);
	}

	public void handleOnActivityResult(int requestCode, int resultCode,
			Intent data) {
		onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 检查错误消息，在Callback方法里使用
	 * 
	 * @param map
	 * @return
	 */
	protected boolean showErrMsg(Map<String, Object> map) {
		Integer errno = (Integer) map.get(HttpUtil.KEY_ERROR_CODE);

		if (errno != HttpUtil.ERROR_SUCCESS) {
			Log.d("Main", "errno:" + errno);
			String errmsg = (String) map.get(HttpUtil.KEY_ERROR_MSG);
			if (!MiscHelper.isEmpty(errmsg)) {
				Toast.makeText(this, errmsg, Toast.LENGTH_SHORT).show();
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Activity parent = getParent();
			if (parent != null) {
				return parent.onKeyDown(keyCode, event);
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
